package top.baselitch.sample;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import top.baselitch.liteorm.DaoExecutor;
import top.baselitch.liteorm.LiteDaoFactory;
import top.baselitch.sample.beans.User;
import top.baselitch.sample.beans.Users;
import top.baselitch.sample.dao.UsersDao;
import top.baselitch.sample.dbutils.DaoFactory;
import top.baselitch.sample.dbutils.UserDao;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener{
    private ActionBar actionBar;
    private String title;
    private int activityTag;
    private EditText userNameEdit,emailEdit,passwdEdit;
    private Button submitBtn;
    private UserDao mDao;
    private User user;
    private UsersDao userDao;
    public static final int TAG_ADD = 0X01;
    public static final int TAG_CHANGE = 0X02;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        actionBar = getSupportActionBar();
        getIntentData();
        actionBar.setTitle(title);
        initView();
        submitBtn.setOnClickListener(this);
        mDao = DaoFactory.getUserDao(this);
        userDao = LiteDaoFactory.create(UsersDao.class);
    }
    private void initView(){
        userNameEdit = (EditText) findViewById(R.id.edit_userName);
        emailEdit = (EditText) findViewById(R.id.edit_userEmail);
        passwdEdit = (EditText) findViewById(R.id.edit_userPasswd);
        submitBtn = (Button) findViewById(R.id.btn_submit);
        if(activityTag == TAG_CHANGE){
            userNameEdit.setText(user.getUserName());
            emailEdit.setText(user.getEmail());
            passwdEdit.setText(user.getPasswd());
        }
    }

    private void getIntentData(){
        Bundle bundle = getIntent().getExtras();
        title = bundle.getString(INTENT.TITLE);
        activityTag = bundle.getInt(INTENT.TAG);
        user = (User) bundle.getSerializable(INTENT.USER);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                submitToDatabase();
                break;
        }
    }

    private void submitToDatabase(){
        String userName = userNameEdit.getText().toString();
        String userEmail = emailEdit.getText().toString();
        String userPasswd = passwdEdit.getText().toString();
        if(verifyInput(new String[]{userName,userEmail,userPasswd})){
            Users users = new Users();
            users.setName(userName);
            users.setEmail(userEmail);
            users.setPasswd(userPasswd);
//            User user = new User();
//            user.setUserName(userName);
//            user.setEmail(userEmail);
//            user.setPasswd(userPasswd);
            if(activityTag==TAG_ADD){
//                mDao.addUser(user);
                DaoExecutor daoex = userDao.addUser(users);
                daoex.exec();
                Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                finish();
            }else if(activityTag==TAG_CHANGE){
                mDao.alterUser(this.user.getUserId(),user);
                Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        }else{
            Toast.makeText(this,"请输入正确的信息",Toast.LENGTH_SHORT).show();
        }
    }

    private boolean verifyInput(String[] input){
        String userName = input[0].replace(" ","");
        String userEmail = input[1].replace(" ","");
        String userPasswd = input[2].replace(" ","");
        if(userName.length()>0&&userName.length()<20){
            if(userEmail.length()>0&&userEmail.length()<20){
                if(userPasswd.length()>0&&userPasswd.length()<20){
                    return true;
                }
            }
        }
        return false;
    }


    public interface INTENT{
        String TITLE = "title";
        String TAG = "activityTag";
        String USER = "userData";
    }
}
